package com.tim.test.download.house;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import groovy.util.logging.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * jQuery选择器规则，https://www.runoob.com/jquery/jquery-ref-selectors.html：
 * 	.选择class，#选择id，[]选择属性，:contains(第)内容包含‘第’
 * 	p,h2；同时选择p、h2
 * 	div.main；同时满足
 * 	div .comment；子孙
 * 	div>p；子级
 * 	div~h1；兄弟
 */
@Slf4j
public class Beike {
    String dir = "D:/个人资料/house/";
    String houseSourceFile =dir+"房源信息.csv";
    String appName=this.getClass().getSimpleName().toLowerCase();
    String appDir = dir+appName+"/";
    String appHtmlDir = appDir +"html/";
    String erShouFangFile = appDir +"erShouFang.csv";
    Set<String> houseSet = new TreeSet<>();
    Map<String, String> map = MapBuilder.<String, String>create()
            .put("unitPrice","单价")
            .put("area","面积")
            .put("jushi","户型")
            .put("resblockName","小区")
            .put("title","描述")
            .put("onListDate","上线日期")
            .put("buildSize","建面")
            .put("price","总价")
            .put("houseCode","编号")
            .build();
    String xiaoXue="没得小学";
    String urlPre="https://cq.ke.com/xiaoqu/";
    int appUrlIndex=2;

	@Test
	public void downloadByPage() throws Exception {
        List<String> houseSourceList = Files.readAllLines(Paths.get(houseSourceFile), Charset.forName("GB2312"));
        Path erShouFangFilePath = Paths.get(erShouFangFile);
        List<String> houseList = Files.readAllLines(erShouFangFilePath, Charset.forName("GB2312"));
        houseSet.addAll(houseList);
        String dateStr = DateUtil.formatDate(new Date());
        boolean flag=false;

        for (int i = 1; i < houseSourceList.size(); i++) {
            // 解析地址
            String[] split = houseSourceList.get(i).trim().split(",");
            xiaoXue =split[0].trim();
            String xiaoQu =split[1].trim();
            String houseSourceUrl = urlPre+split[appUrlIndex].trim();

            try {
                Document document = Jsoup.connect(houseSourceUrl).get();
                Files.write(Paths.get(appHtmlDir, String.join("_",xiaoXue, xiaoQu, dateStr)), Arrays.asList(document.html()));
                Elements scripts = document.getElementsByTag("script");
                for(Element script : scripts){
                    String scriptHtml = script.html().trim();
                    if(scriptHtml.startsWith("window.GLOBAL_INFOS")){
                        Set<String> set = parseErShouFang(scriptHtml);
                        flag = houseSet.addAll(set)||flag;
                        Files.write(erShouFangFilePath, houseSet);
                        System.out.println(String.format("下载成功，小学：%s，小区：%s，成交信息：%s", xiaoXue, xiaoQu, houseSet));
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("SUCCESS，"+(flag?"有新纪录":""));

	}


//    @Test
    public void t() throws IOException {
        String str="window.GLOBAL_INFOS = {\n" +
                "    id:3611057906237,\n" +
                "    resblockPosition:'106.46029139127,29.572310876858',\n" +
                "    resblockName:'天越雅园',\n" +
                "    resblockId: '3611057906237',\n" +
                "    cityId: '500000',\n" +
                "    others: {\"name\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"resblockId\":3611057906237,\"favCount\":63,\"minTenement\":1.2,\"maxTenement\":1.21,\"buildingCount\":6,\"houseAmount\":979,\"curbageRate\":3.18,\"greenRate\":33,\"address\":\"\\u767d\\u9e64\\u5cad3\\u53f7, \\u767d\\u9e64\\u5cad1\\u53f7\",\"buildingType\":\"\\u5854\\u697c\\/\\u677f\\u697c\",\"buildYear\":\"2000-2012\\u5e74\",\"property\":\"\\u91cd\\u5e86\\u4e3d\\u9526\\u7269\\u4e1a\\u7ba1\\u7406\\u6709\\u9650\\u516c\\u53f8\",\"developer\":\"\\u6df1\\u5733\\u5e02\\u5929\\u8d8a\\u7269\\u4e1a\\u53d1\\u5c55\\u6709\\u9650\\u516c\\u53f8\",\"agentShop\":\"\\u6069\\u5c45-\\u91d1\\u805a\\u9f0e-\\u9f99\\u51e4\\u4e91\\u6d32\\u5e97\",\"shopAddress\":\"\\u5927\\u6768\\u516c\\u6865104\\u53f7\\u964421\\u53f7\",\"shopDistance\":\"565m\",\"shopPosition\":\"106.456538,29.568421\",\"district\":\"\\u6c99\\u576a\\u575d\",\"districtUrl\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/shapingba\\/\",\"bizcircle\":\"\\u6768\\u516c\\u6865\",\"bizcircleUrl\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/yanggongqiao\\/\",\"coordinates\":\"106.46029139127,29.572310876858\",\"unitPrice\":9204,\"bdaTime\":\"2\\u6708\",\"houseSellNum\":31,\"dealProperties\":\"\\u5546\\u54c1\\u623f\\/\\u623f\\u6539\\u623f\\/\\u7ecf\\u6d4e\\u9002\\u7528\\u623f\\/\\u62c6\\u8fc1\\u8fd8\\u5efa\\u623f\",\"priceDesc\":\"\\u5efa\\u9762\\u5355\\u4ef7\",\"electricType\":\"\\u6c11\\u7535\",\"heatingType\":\"\\u6682\\u65e0\\u4fe1\\u606f\",\"waterType\":\"\\u6c11\\u6c34\",\"isSellPrice\":2,\"priceUnitAvgGov\":0},\n" +
                "    father_others: {\"subNav\":[{\"title\":\"\\u5728\\u552e\",\"url\":\"\\/ershoufang\\/\",\"current\":false,\"isNew\":false},{\"title\":\"\\u6210\\u4ea4\",\"url\":\"\\/chengjiao\\/\",\"current\":false,\"isNew\":true},{\"title\":\"\\u5c0f\\u533a\",\"url\":\"\\/xiaoqu\\/\",\"current\":1,\"isNew\":false},{\"title\":\"\\u5730\\u56fe\\u627e\\u623f\",\"url\":\"https:\\/\\/map.ke.com\\/map\\/500000\\/ESF\\/\",\"current\":false,\"isNew\":false}],\"abstract\":{\"name\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"resblockId\":3611057906237,\"favCount\":63,\"minTenement\":1.2,\"maxTenement\":1.21,\"buildingCount\":6,\"houseAmount\":979,\"curbageRate\":3.18,\"greenRate\":33,\"address\":\"\\u767d\\u9e64\\u5cad3\\u53f7, \\u767d\\u9e64\\u5cad1\\u53f7\",\"buildingType\":\"\\u5854\\u697c\\/\\u677f\\u697c\",\"buildYear\":\"2000-2012\\u5e74\",\"property\":\"\\u91cd\\u5e86\\u4e3d\\u9526\\u7269\\u4e1a\\u7ba1\\u7406\\u6709\\u9650\\u516c\\u53f8\",\"developer\":\"\\u6df1\\u5733\\u5e02\\u5929\\u8d8a\\u7269\\u4e1a\\u53d1\\u5c55\\u6709\\u9650\\u516c\\u53f8\",\"agentShop\":\"\\u6069\\u5c45-\\u91d1\\u805a\\u9f0e-\\u9f99\\u51e4\\u4e91\\u6d32\\u5e97\",\"shopAddress\":\"\\u5927\\u6768\\u516c\\u6865104\\u53f7\\u964421\\u53f7\",\"shopDistance\":\"565m\",\"shopPosition\":\"106.456538,29.568421\",\"district\":\"\\u6c99\\u576a\\u575d\",\"districtUrl\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/shapingba\\/\",\"bizcircle\":\"\\u6768\\u516c\\u6865\",\"bizcircleUrl\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/yanggongqiao\\/\",\"coordinates\":\"106.46029139127,29.572310876858\",\"unitPrice\":9204,\"bdaTime\":\"2\\u6708\",\"houseSellNum\":31,\"dealProperties\":\"\\u5546\\u54c1\\u623f\\/\\u623f\\u6539\\u623f\\/\\u7ecf\\u6d4e\\u9002\\u7528\\u623f\\/\\u62c6\\u8fc1\\u8fd8\\u5efa\\u623f\",\"priceDesc\":\"\\u5efa\\u9762\\u5355\\u4ef7\",\"electricType\":\"\\u6c11\\u7535\",\"heatingType\":\"\\u6682\\u65e0\\u4fe1\\u606f\",\"waterType\":\"\\u6c11\\u6c34\",\"isSellPrice\":2,\"priceUnitAvgGov\":0},\"is_bk\":false,\"bigPic\":[\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/2613a137-b7c5-4976-9b98-ce954ff326e9.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/fd690b69-1922-4e95-8f88-dae0d5c7d6e1.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/ce7ba5a5-ccf9-40b9-b934-6b9dd2e216b8.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/4cfa3b74-c9c2-4bc7-a193-d510ee90dccc.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/9a780763-5cef-44f4-935f-76ba1de46f1e.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/c2438f34-c231-4b45-92db-7db987e50da2.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/5d2a57c0-a6e5-4e1d-9531-90c2fe8e3798.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/40f134bc-95f7-4adb-b67a-b436b16acf8c.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/dbbb5027-3b67-4dee-a96f-cbdf1c8a7e23.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/2e80e72d-631d-4c0a-8962-35d28a00d04a.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/b65c026f-77bb-44be-8e75-ac4c7680125b.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/be40b085-978d-4bec-814e-e8a7bfc08229.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/fef4f180-5ade-426a-9439-f1327f901056.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/a9f8fd27-a2ac-4df2-8713-c35c27603325.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/a47ef522-4e1b-4626-8f14-ac7183a8bc57.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/fb16ef69-6c85-4a86-b368-f78b69993c6d.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/fd673b18-dcf2-4403-9766-b4057ec68df2.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/9e8428eb-7757-4df5-9074-b4bf79e349e5.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/83ea5ae3-8477-4cc1-8a7e-bd7804cc1771.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/8f77568e-2947-47bf-96e9-1c1fa22c2ea1.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/452dbd6b-0ebd-4fbf-9268-09e737875289.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/ff5ae8f3-21ed-4b14-885f-1ed72550ce97.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/b6ecdd33-f94b-4814-ae25-be8fc11d0266.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/dc6a40fd-0dc7-4967-8fd2-4d7b7ccab8ee.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/eeead925-63ac-4fba-ac72-c68c1678d2cf.jpg\",\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/eee6bad1-d565-4288-85d6-9c20cddfdc24.jpg\"],\"picNum\":26,\"panorama\":{\"imgUrl\":\"https:\\/\\/img.ljcdn.com\\/element-library\\/prod-ae7ed08e-aa28-477c-8030-5eb288e1a1ce.png\",\"iconUrl\":\"https:\\/\\/img.ljcdn.com\\/beike\\/buiness2c\\/1605168855777.gif\",\"actionUrl\":\"https:\\/\\/mr.ke.com\\/3dmvp\\/#\\/574965?source=RESBLOCK\"},\"sold\":[{\"houseId\":\"106114031371\",\"viewUrl\":\"https:\\/\\/cq.ke.com\\/chengjiao\\/106114031371.html\",\"source\":\"\",\"jushi\":\"2\\u5ba41\\u5385\",\"area\":52.13,\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"floorStat\":\"\\u6682\\u65e0\\u6570\\u636e\",\"totalFloor\":\"17\",\"signTime\":\"2024-07-05\",\"isSold\":1,\"orientation\":\"\\u6682\\u65e0\\u6570\\u636e\",\"isDisplay\":1,\"signSource\":\"\",\"buildYear\":\"2000\\u5e74\",\"title\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"price\":37.9,\"unitPrice\":7267,\"imgSrc\":null},{\"houseId\":\"106113506183\",\"viewUrl\":\"https:\\/\\/cq.ke.com\\/chengjiao\\/106113506183.html\",\"source\":\"\",\"jushi\":\"2\\u5ba42\\u5385\",\"area\":84.26,\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"floorStat\":\"\\u6682\\u65e0\\u6570\\u636e\",\"totalFloor\":\"33\",\"signTime\":\"2023-03-02\",\"isSold\":1,\"orientation\":\"\\u6682\\u65e0\\u6570\\u636e\",\"isDisplay\":1,\"signSource\":\"\",\"buildYear\":\"2008\\u5e74\",\"title\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"price\":81.6,\"unitPrice\":9685,\"imgSrc\":null},{\"houseId\":\"106104725278\",\"viewUrl\":\"https:\\/\\/cq.ke.com\\/chengjiao\\/106104725278.html\",\"source\":\"\",\"jushi\":\"1\\u5ba41\\u5385\",\"area\":52.13,\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"floorStat\":\"\\u6682\\u65e0\\u6570\\u636e\",\"totalFloor\":\"17\",\"signTime\":\"2021-06-13\",\"isSold\":1,\"orientation\":\"\\u6682\\u65e0\\u6570\\u636e\",\"isDisplay\":1,\"signSource\":\"\",\"buildYear\":\"2000\\u5e74\",\"title\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"price\":62.5,\"unitPrice\":11990,\"imgSrc\":null}],\"soldUrl\":\"https:\\/\\/cq.ke.com\\/chengjiao\\/c3611057906237\\/\",\"rent\":[{\"viewUrl\":\"http:\\/\\/cq.zu.ke.com\\/zufang\\/CQ1959487827972456448.html\",\"price\":\"1400\",\"source\":\"zufang\",\"square\":\"67.00\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/110000-inspection\\/pc1_hTqeMqJo0.jpg.280x210.jpg\",\"roomNum\":\"2\\u5ba41\\u53851\\u536b\",\"title\":\"\\u6574\\u79df\\u00b7\\u5929\\u8d8a\\u96c5\\u56ed 2\\u5ba41\\u5385 \\u5357\"},{\"viewUrl\":\"http:\\/\\/cq.zu.ke.com\\/zufang\\/CQ1960921645891715072.html\",\"price\":\"1500\",\"source\":\"zufang\",\"square\":\"97.00\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/110000-inspection\\/324bdf12-0c08-4003-a8af-600e16480b60.jpg.280x210.jpg\",\"roomNum\":\"3\\u5ba42\\u53851\\u536b\",\"title\":\"\\u6574\\u79df\\u00b7\\u5929\\u8d8a\\u96c5\\u56ed 3\\u5ba42\\u5385 \\u4e1c\\u5357\"},{\"viewUrl\":\"http:\\/\\/cq.zu.ke.com\\/zufang\\/CQ1916141205335310336.html\",\"price\":\"1500\",\"source\":\"zufang\",\"square\":\"90.00\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/rent-front-image\\/23f6f5a78a527d819b1579a7a0a7c062.png.280x210.jpg\",\"roomNum\":\"2\\u5ba41\\u53851\\u536b\",\"title\":\"\\u6574\\u79df\\u00b7\\u5929\\u8d8a\\u96c5\\u56ed 2\\u5ba41\\u5385 \\u4e1c\\u5357\"}],\"rentUrl\":\"http:\\/\\/cq.zu.ke.com\\/zufang\\/c3611057906237\\/\",\"ershoufang\":[{\"id\":\"106116417306\",\"title\":\"\\u6b63\\u89c4\\u4e24\\u623f \\u6237\\u578b\\u65b9\\u6b63 \\u91c7\\u5149\\u597d \\u89c6\\u91ce\\u5f00\\u9614\",\"price\":\"68\",\"unitPrice\":\"7987\",\"area\":85.14,\"viewUrl\":\"https:\\/\\/cq.ke.com\\/ershoufang\\/106116417306.html\",\"onListDate\":\"2023-11-02\",\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"resblockUrl\":\"\",\"picture\":\"https:\\/\\/ke-image.ljcdn.com\\/110000-inspection\\/pc1_uoDGP0d83.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com\",\"jushi\":\"2\\u5ba42\\u5385\",\"buildSize\":85.14,\"hallNum\":\"2\\u5ba42\\u5385\",\"houseCode\":\"106116417306\"},{\"id\":\"106118160842\",\"title\":\"\\u4e09\\u5ce1\\u5e7f\\u573a\\u6768\\u516c\\u6865\\u5730\\u94c1\\u7ad9 \\u7cbe\\u88c5\\u5c0f2\\u623f \\u914d\\u5957\\u5357\\u5f00\",\"price\":\"57\",\"unitPrice\":\"10258\",\"area\":55.57,\"viewUrl\":\"https:\\/\\/cq.ke.com\\/ershoufang\\/106118160842.html\",\"onListDate\":\"2024-04-02\",\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"resblockUrl\":\"\",\"picture\":\"https:\\/\\/ke-image.ljcdn.com\\/110000-inspection\\/pc1_qSnxAt27m.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com\",\"jushi\":\"2\\u5ba41\\u5385\",\"buildSize\":55.57,\"hallNum\":\"2\\u5ba41\\u5385\",\"houseCode\":\"106118160842\"},{\"id\":\"106110433985\",\"title\":\"\\u6b63\\u89c4\\u4e00\\u5ba4\\u4e00\\u5385\\uff0c\\u7535\\u68af\\u623f\\uff0c\\u4f4d\\u7f6e\\u597d\\u3002\",\"price\":\"38\",\"unitPrice\":\"7985\",\"area\":47.59,\"viewUrl\":\"https:\\/\\/cq.ke.com\\/ershoufang\\/106110433985.html\",\"onListDate\":\"2022-02-16\",\"resblockName\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"resblockUrl\":\"\",\"picture\":\"https:\\/\\/ke-image.ljcdn.com\\/110000-inspection\\/pc1_ynzOq7K8t_1.jpg!m_fill,w_280,h_210,f_jpg?from=ke.com\",\"jushi\":\"1\\u5ba41\\u5385\",\"buildSize\":47.59,\"hallNum\":\"1\\u5ba41\\u5385\",\"houseCode\":\"106110433985\"}],\"ershoufangUrl\":\"https:\\/\\/cq.ke.com\\/ershoufang\\/c3611057906237\\/\",\"nearby\":[{\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611057810126\\/\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/ae6b3bd1-8b17-44aa-81aa-6ebb46c29f10.jpg\",\"name\":\"\\u84dd\\u6eaa\\u8c37\\u5730\",\"houseType\":\"\\u8f66\\u5e93\\/\\u666e\\u901a\\u4f4f\\u5b85\\/\\u5546\\u4e1a\\/\\u5e95\\u5546\",\"sellNum\":311,\"unitPrice\":\"11155\\u5143\\/\\u33a1\"},{\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611057077012\\/\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/9d277044-adec-41cd-8c70-e612c9760a62.jpg\",\"name\":\"\\u4e1c\\u6d77\\u5cb8\",\"houseType\":\"\\u8f66\\u5e93\\/\\u666e\\u901a\\u4f4f\\u5b85\\/\\u5546\\u4e1a\\/\\u5e95\\u5546\",\"sellNum\":232,\"unitPrice\":\"11194\\u5143\\/\\u33a1\"},{\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/36000000068346\\/\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/053723c3-2e8d-4c08-a4c0-1c937ca6685a.JPG\",\"name\":\"\\u4e2d\\u7cae\\u5929\\u60a6\\u58f9\\u53f7\\u60a6\\u749f\",\"houseType\":\"\\u666e\\u901a\\u4f4f\\u5b85\\/\\u5546\\u4e1a\\/\\u5e95\\u5546\",\"sellNum\":217,\"unitPrice\":\"14984\\u5143\\/\\u33a1\"},{\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611099958876\\/\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/2ea000e2-6b43-4970-8b55-8e21282d9eba.JPG\",\"name\":\"\\u58f9\\u6c5f\\u57ce\",\"houseType\":\"\\u8f66\\u5e93\\/\\u666e\\u901a\\u4f4f\\u5b85\\/\\u522b\\u5885\\/\\u5e95\\u5546\",\"sellNum\":195,\"unitPrice\":\"12449\\u5143\\/\\u33a1\"},{\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611057525571\\/\",\"imgSrc\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/13680238-ce49-4d20-b732-391f98c4519d.jpg\",\"name\":\"\\u4e0a\\u7b51\\u878d\\u6c47\\u56fd\\u9645\\u6e29\\u6cc9\",\"houseType\":\"\\u8f66\\u5e93\\/\\u666e\\u901a\\u4f4f\\u5b85\\/\\u5e95\\u5546\",\"sellNum\":195,\"unitPrice\":\"11106\\u5143\\/\\u33a1\"}],\"tdkData\":{\"title\":\"\\u5929\\u8d8a\\u96c5\\u56ed_\\u91cd\\u5e86\\u5929\\u8d8a\\u96c5\\u56ed\\u4e8c\\u624b\\u623f|\\u623f\\u4ef7|\\u79df\\u623f\\u3010\\u91cd\\u5e86\\u8d1d\\u58f3\\u627e\\u623f\\u3011\",\"keywords\":\"\\u5929\\u8d8a\\u96c5\\u56ed,\\u91cd\\u5e86\\u5929\\u8d8a\\u96c5\\u56ed,\\u5929\\u8d8a\\u96c5\\u56ed\\u4e8c\\u624b\\u623f,\\u5929\\u8d8a\\u96c5\\u56ed\\u623f\\u4ef7,\\u5929\\u8d8a\\u96c5\\u56ed\\u79df\\u623f \",\"description\":\"\\u8d1d\\u58f3\\u91cd\\u5e86\\u5c0f\\u533a\\u5927\\u5168,\\u5929\\u8d8a\\u96c5\\u56ed\\u53c2\\u8003\\u5747\\u4ef7:9204\\u5143\\/\\u33a1,\\u4f4d\\u4e8e\\u6c99\\u576a\\u575d\\u6768\\u516c\\u6865,\\u5929\\u8d8a\\u96c5\\u56ed\\u5728\\u552e\\u4e8c\\u624b\\u623f\\u6e9031\\u5957,\\u5df2\\u670963\\u4eba\\u5173\\u6ce8.\\u67e5\\u8be2\\u5929\\u8d8a\\u96c5\\u56ed\\u4f18\\u8d28\\u4e8c\\u624b\\u623f\\u623f\\u6e90\\u3001\\u79df\\u623f\\u623f\\u6e90\\u3001\\u7ecf\\u7eaa\\u4eba\\u3001\\u8fd1\\u671f\\u6210\\u4ea4\\u8bb0\\u5f55\\u7b49\\u4fe1\\u606f,\\u5c31\\u5230\\u91cd\\u5e86\\u8d1d\\u58f3\\u627e\\u623f.\",\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611057906237\\/\",\"firstImg\":\"https:\\/\\/ke-image.ljcdn.com\\/hdic-resblock\\/2613a137-b7c5-4976-9b98-ce954ff326e9.jpg\",\"pubDate\":\"2018-04-30T17:45:01\",\"upDate\":\"2025-03-06T20:08:28\"},\"canonical\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/3611057906237\\/\",\"breadcrumbs\":{\"0\":{\"title\":\"\\u91cd\\u5e86\\u623f\\u4ea7\",\"url\":\"\\/\"},\"1\":{\"title\":\"\\u91cd\\u5e86\\u5c0f\\u533a\",\"url\":\"\\/xiaoqu\\/\"},\"3\":{\"title\":\"\\u6768\\u516c\\u6865\\u5c0f\\u533a\",\"url\":\"https:\\/\\/cq.ke.com\\/xiaoqu\\/yanggongqiao\\/\"},\"4\":{\"title\":\"\\u5929\\u8d8a\\u96c5\\u56ed\",\"url\":\"\\/xiaoqu\\/3611057906237\\/\"}},\"meta\":{\"curCityId\":500000,\"curCityName\":\"\\u91cd\\u5e86\"},\"fbExpoId\":\"\",\"fbQueryId\":\"\",\"isShowBriefData\":true},\n" +
                "    isShowBriefData: '',\n" +
                "    // sidebar:null\n" +
                "  }" +
                "";
        Set<String> set = parseErShouFang(str);
        System.out.println("Variable ershoufang: " + JSON.toJSONString(set));
    }

    public Set<String> parseErShouFang(String info) throws IOException {
        info=info.replaceFirst("window\\.","")
                .replaceAll(",\\s+// sidebar:null","");
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");

        try {
            // 执行脚本
            engine.eval(info);

            // 获取变量值
            Object globalInfos = engine.get("GLOBAL_INFOS");
//            System.out.println("Variable globalInfos: " + JSON.toJSONString(globalInfos));
            JSONObject global = JSON.parseObject(JSON.toJSONString(globalInfos));
            JSONObject ershoufang = global.getJSONObject("father_others")
                    .getJSONObject("ershoufang");
            Set<String> set = ershoufang.keySet()
                    .stream()
                    .map(key -> {
                        JSONObject house = ershoufang.getJSONObject(key);
//                                .put("unitPrice","单价")
//                                .put("area","面积")
//                                .put("jushi","户型")
//                                .put("resblockName","小区")
//                                .put("title","描述")
//                                .put("onListDate","上线日期")
//                                .put("buildSize","建面")
//                                .put("price","总价")
//                                .put("houseCode","编号")
                        return String.join(",", xiaoXue
                                , house.getString("resblockName")
                                , house.getString("jushi")
                                , house.getString("buildSize")
                                , house.getString("price")
                                , house.getString("area")
                                , house.getString("unitPrice")
                                , house.getString("onListDate")
                                , house.getString("title")
                                , house.getString("houseCode")
                        );
                    }).collect(Collectors.toSet());
            return set;
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

}
